package cc.htdf.gwc.observation.repository;

import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JT on 2017/10/11.
 */
@Repository
public class ObserveDataRepositoryImpl implements ObserveDataRepository {

    public static final String TABLE_NAME_DATA_OBSERVATION = "DATA_OBSERVATION_HOUR";
    public static final String[] TABLE_BASE_COLUMNS_OBSERVATION = new String[]{
            "SITE_ID", "OBSERVATION_DATE", "TIME"
    };


    @Autowired
    private NamedParameterJdbcTemplate nameJdbcTemplate;


    /**
     * 根据站号、观测时间获取观测数据
     *
     * @param stationList ：站号
     * @param date     ：观测时间
     * @param elements : 要素
     * @return
     */
    @Override
    public List<Map<String, Object>> fetchObserveDataByStationsAndDate(List<String> stationList, String date, String elements) {
        String baseColumns = Joiner.on(',').join(TABLE_BASE_COLUMNS_OBSERVATION);
        String sql = String.format("SELECT %s,%s FROM %s WHERE OBSERVATION_DATE=:observationDate and SITE_ID IN (:stations)",
                    baseColumns, elements, TABLE_NAME_DATA_OBSERVATION);
        Map<String, Object> params = new HashMap<String, Object>(){{
            put("stations", stationList);
            put("observationDate", date);
        }};
        return nameJdbcTemplate.queryForList(sql, params);
    }

    /**
     * 根据站号、观测时间获取观测数据
     *
     * @param stationList ：站号
     * @param startDate     ：观测时间
     * @param elements : 要素
     * @return
     */
    @Override
    public List<Map<String, Object>> observeData(List<String> stationList, String startDate,String endDate, String elements) {
        String baseColumns = Joiner.on(',').join(TABLE_BASE_COLUMNS_OBSERVATION);
        String sql = String.format("SELECT %s,%s FROM %s WHERE OBSERVATION_DATE>=:startDate and OBSERVATION_DATE<=:endDate and SITE_ID IN (:stations)",
                baseColumns, elements, TABLE_NAME_DATA_OBSERVATION);
        Map<String, Object> params = new HashMap<String, Object>(){{
            put("stations", stationList);
            put("startDate", startDate);
            put("endDate", endDate);
        }};
        return nameJdbcTemplate.queryForList(sql, params);
    }

    /**
     * 根据站号获取观测数据
     *
     * @param station ：站号
     * @return
     */
    @Override
    public List<Map<String, Object>> fetchChengDuObserveDataByStationId(String station, String elements) {
        String baseColumns = Joiner.on(',').join(TABLE_BASE_COLUMNS_OBSERVATION);
        String sql = String.format("SELECT %s,%s FROM %s WHERE SITE_ID=:station",
                baseColumns, elements, TABLE_NAME_DATA_OBSERVATION);
        Map<String, Object> params = new HashMap<String, Object>() {{
           put("station", station);
        }};
        return nameJdbcTemplate.queryForList(sql, params);
    }
}
